package jp.co.htkk.service.impl;

import jp.co.htkk.common.CommonUtil;
import jp.co.htkk.common.enums.ResultCode;
import jp.co.htkk.common.exception.BusinessException;
import jp.co.htkk.dto.${TableName!}Dto;
import jp.co.htkk.dto.UserDto;
import jp.co.htkk.entity.${TableName!};
import jp.co.htkk.form.${TableName!}RegisterForm;
import jp.co.htkk.repository.${TableName!}Repository;
import jp.co.htkk.service.${TableName!}Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class ${TableName!}ServiceImpl implements ${TableName!}Service {

    private DozerBeanMapper beanMapper = new DozerBeanMapper();
    @Autowired
    private ${TableName!}Repository ${tableName!}Repository;

    /**
     * @param ${tableName!}RegisterForm
     * @return jp.co.htkk.dto.${TableName!}Dto
     * @throws
     */
    @Override
    public ${TableName!}Dto register(${TableName!}RegisterForm ${tableName!}RegisterForm){
        log.info("Start ${tableName!} register");
        //登录时间、更新时间获得
        CommonUtil commonUtil = new CommonUtil();
        Timestamp nowTimestamp =commonUtil.getNowTimestamp();
        ${TableName!} ${tableName!} = new ${TableName!}();
<#list  columns_remove_ingore as field>
    <#if field.isPk=="1">
    <#else>
        ${tableName!}.set${field.ColumnName!}(${tableName!}RegisterForm.get${field.ColumnName!}());
    </#if>
</#list>
        ${tableName!}.setDeleteFlag(0);
        ${tableName!}.setRegisterDateTime(nowTimestamp);
        ${tableName!}.setUpdateDateTime(nowTimestamp);

        ${tableName!}Repository.create${TableName!}(${tableName!});

        ${TableName!}Dto ${tableName!}Dto = beanMapper.map(${tableName!}, ${TableName!}Dto.class);

        log.info("Finish ${tableName!} register");
        return ${tableName!}Dto;
    }

    /**
     * @param id
     * @return jp.co.htkk.dto.${TableName!}Dto
     */
    @Override
    public ${TableName!}Dto get${TableName!}ById(String id) {
        log.info("get ${tableName!} with id {}", id);
        return ${tableName!}Repository.get${TableName!}ById(id);
    }

    /**
     * @param id
     * @return jp.co.htkk.dto.${TableName!}Dto
     */
    @Override
    public ${TableName!}Dto delete${TableName!}ById(String id) {
        log.info("delete ${tableName!} with id {}", id);
        ${TableName!}Dto ${tableName!}Dto = ${tableName!}Repository.get${TableName!}ById(id);
        ${tableName!}Repository.delete${TableName!}ById(id);
        return ${tableName!}Dto;
    }

    /**
     * @param ${tableName!}Dto
     * @return jp.co.htkk.dto.${TableName!}Dto
     */
    @Override
    public ${TableName!}Dto update${TableName!}(${TableName!}Dto ${tableName!}Dto) {
        log.info("update ${tableName!} with id {}", ${tableName!}Dto.getId());
        if (Objects.isNull(${tableName!}Dto.getId())) {
            throw new BusinessException(ResultCode.VALIDATE_FAILED);
        }
        ${tableName!}Repository.update${TableName!}(${tableName!}Dto);
        ${TableName!}Dto ${tableName!} = ${tableName!}Repository.get${TableName!}ById(${tableName!}Dto.getId().toString());
        return ${tableName!};
    }
}
